#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<vector>
using namespace std;
namespace MyVector
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finsh;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finsh;
		}
		vector()
			:_start(nullptr),
			_finsh(nullptr),
			_endofstorage(nullptr)
		{
		}
		vector(initializer_list<T> il)
			:_start(nullptr),
			_finsh(nullptr),
			_endofstorage(nullptr)
		{
			reverse(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
			:_start(nullptr),
			_finsh(nullptr),
			_endofstorage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, const T& val = T())
		{
			reverse(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reverse(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(const vector<T>& v)
		{
			reverse(v.capacity());
			for (auto& x : v)
			{
				push_back(x);
			}
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finsh, v._finsh);
			std::swap(_endofstorage, v._endofstorage);
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		void resize(size_t n, T val = T())
		{
			if (n < size())
			{
				while (_finsh > _start + n)
				{
					_finsh--;
				}
			}
			else {
				reverse(n);
				while (_finsh < _start + n)
				{
					*_finsh = val;
					_finsh++;
				}
			}
		}

		~vector()
		{
			delete[] _start;
			_start = _finsh = _endofstorage = nullptr;
		}
		size_t size() const
		{
			return _finsh - _start;
		}
		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		T& operator[](size_t i)
		{
			assert(i < size());
			return *(_start + i);
		}
		const T& operator[](size_t i) const
		{
			assert(i < size());
			return *(_start + i);
		}
		void reverse(size_t n)
		{
			if (n > capacity())
			{
				size_t oldsize = size();
				T* tmp = new T[n];
				if (_start) {
					//memcpy(tmp, _start, sizeof(T) * oldsize);
					for (size_t i = 0; i < oldsize; i++)
					{
						tmp[i] = _start[i];
					}
				}
				delete[] _start;
				_start = tmp;
				_finsh = _start + oldsize;
				_endofstorage = _start + n;
			}
		}
		void push_back(const T& x)
		{
			if (_finsh == _endofstorage)
			{
				reverse(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finsh = x;
			++_finsh;
		}
		bool empty() const
		{
			return _start == _finsh;
		}
		void pop_back()
		{
			assert(!empty());
			--_finsh;
		}
		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finsh);
			size_t oldsize = pos - _start;
			if (_finsh == _endofstorage)
			{
				reverse(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + oldsize;
			}
			iterator it = _finsh;
			while (it > pos)
			{
				*it = *(it - 1);
				--it;
			}
			*pos = x;
			++_finsh;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finsh);
			iterator i = pos + 1;
			while (i < _finsh)
			{
				*(i - 1) = *i;
				i++;
			}
			--_finsh;
			return pos;
		}

	private:
		iterator _start;
		iterator _finsh;
		iterator _endofstorage;
	};
}
